package com.heima.homeservicemanagementsystem.controller;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.heima.homeservicemanagementsystem.config.annotation.RestControllerMapping;
import com.heima.homeservicemanagementsystem.module.Result;
import com.heima.homeservicemanagementsystem.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.List;

/**
 * @Description
 * @Author jcf
 * @Date 2025/3/5
 */
@Tag(name = "测试接口")
@RestControllerMapping
public class userMenuController {
    @Resource
    private SysUserService userService;


    @GetMapping("/user/info")
    public Result test() {
        // 创建一个 JSONObject 对象
        JSONObject user = new JSONObject();

        // 向 JSONObject 中添加数据
        user.put("id", "4291d7da9005377ec9aec4a71ea837f");
        user.put("name", "天野远子");
        user.put("username", "admin");
        user.put("password", "");
        user.put("avatar", "/avatar2.jpg");
        user.put("status", 1);
        user.put("telephone", "");
        user.put("lastLoginIp", "27.154.74.117");
        user.put("lastLoginTime", 1534837621348L);
        user.put("creatorId", "admin");
        user.put("createTime", 1497160610259L);
        user.put("merchantCode", "TLif2btpzg079h15bk");
        user.put("deleted", 0);
        user.put("roleId", "admin");
//        ==
        JSONObject roleObj = new JSONObject();
        roleObj.put("id", "admin");
        roleObj.put("name", "管理员");
        roleObj.put("describe", "拥有所有权限");
        roleObj.put("status", 1);
        roleObj.put("creatorId", "system");
        roleObj.put("createTime", 1497160610259L);
        roleObj.put("deleted", 0);

        // 创建 permissions 数组
        JSONArray permissions = new JSONArray();
        JSONObject permission = new JSONObject();
        permission.put("roleId", "admin");
        permission.put("permissionId", "dashboard");
        permission.put("permissionName", "仪表盘");

        // 将 actions 字符串解析为 JSON 数组
        String actionsString = "[{\"action\":\"add\",\"defaultCheck\":false,\"describe\":\"新增\"},"
                + "{\"action\":\"query\",\"defaultCheck\":false,\"describe\":\"查询\"},"
                + "{\"action\":\"get\",\"defaultCheck\":false,\"describe\":\"详情\"},"
                + "{\"action\":\"update\",\"defaultCheck\":false,\"describe\":\"修改\"},"
                + "{\"action\":\"delete\",\"defaultCheck\":false,\"describe\":\"删除\"}]";
        JSONArray actions = new JSONArray(actionsString);
        permission.put("actions", actions);

        // 创建 actionEntitySet 数组
        JSONArray actionEntitySet = new JSONArray();
        actionEntitySet.put(new JSONObject().put("action", "add").put("describe", "新增").put("defaultCheck", false));
        actionEntitySet.put(new JSONObject().put("action", "query").put("describe", "查询").put("defaultCheck", false));
        actionEntitySet.put(new JSONObject().put("action", "get").put("describe", "详情").put("defaultCheck", false));
        actionEntitySet.put(new JSONObject().put("action", "update").put("describe", "修改").put("defaultCheck", false));
        actionEntitySet.put(new JSONObject().put("action", "delete").put("describe", "删除").put("defaultCheck", false));
        permission.put("actionEntitySet", actionEntitySet);

        // 添加到 permissions 数组
        permissions.put(permission);

        // 将 permissions 添加到 roleObj 中
        roleObj.put("permissions", permissions);

        user.put("role", roleObj);

        // 输出 JSONObject 的内容
//            System.out.println("User Information:");
//            System.out.println(user.toString()); // 使用缩进格式输出 JSON
//            System.out.println(user); // 使用缩进格式输出 JSON
        return Result.success(user);
    }

    //动态路由没有相关的角色分配
    @GetMapping("/user/nav")
    @Operation(summary = "获取用户的菜单")
    public Result<List<JSONObject>> userNav() {

//        JSONArray nav = new JSONArray();
//
//        // dashboard
//        JSONObject dashboard = new JSONObject();
//        dashboard.put("name", "dashboard");
//        dashboard.put("parentId", 0);
//        dashboard.put("id", 1);
//
//        // meta 信息
//        JSONObject dashboardMeta = new JSONObject();
//        dashboardMeta.put("title", "menu.dashboard");
//        dashboardMeta.put("icon", "dashboard");
//        dashboardMeta.put("show", true);
//        dashboard.put("meta", dashboardMeta);
//
//        dashboard.put("component", "RouteView");
//        dashboard.put("redirect", "/dashboard/workplace");
//
//        nav.put(dashboard);
//
//        // workplace
//        JSONObject workplace = new JSONObject();
//        workplace.put("name", "workplace");
//        workplace.put("parentId", 1);
//        workplace.put("id", 7);
//
//        // meta 信息
//        JSONObject workplaceMeta = new JSONObject();
//        workplaceMeta.put("title", "menu.dashboard.monitor");
//        workplaceMeta.put("show", true);
//        workplace.put("meta", workplaceMeta);
//
//        workplace.put("component", "Workplace");
//        nav.put(workplace);
//
//
//        //'Analysis'
//        JSONObject Analysis = new JSONObject();
//        Analysis.put("name", "Analysis");
//        Analysis.put("parentId", 1);
//        Analysis.put("id", 2);
//        // meta 信息
//        JSONObject AnalysisMeta = new JSONObject();
//        AnalysisMeta.put("title", "menu.dashboard.analysis");
//        AnalysisMeta.put("show", true);
//        Analysis.put("meta", AnalysisMeta);
//
//        Analysis.put("component", "Analysis");
//        Analysis.put("path", "/dashboard/analysis");
//        nav.put(Analysis);
//
//        return Result.success(nav);
        SaSession saSession = StpUtil.getSessionByLoginId(StpUtil.getLoginId());
        List<JSONObject> list = saSession.get("menu", () -> userService.listmenu());
        return Result.success(list);

    }
}
